package com.itbaizhan.Work;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

//消费者
public class Consume3 {
    public static void main(String[] args) throws IOException, TimeoutException {
        //创建连接工厂
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("192.168.66.101"); //连接的ip地址
        connectionFactory.setPort(5672);
        connectionFactory.setUsername("itbaizhan");
        connectionFactory.setPassword("itbaizhan");
        connectionFactory.setVirtualHost("/");
        //创建连接
        Connection connection = connectionFactory.newConnection();
        //建立信道
        Channel channel = connection.createChannel();
        //监听队列f，处理消费者
        /*
         * 参数1：监听队列名
         * 参数2：是否自动签收，如果设置为false,则需要手动确认消息已接收，否则MQ会一直发送消息
         * 参数3：consumer的实现类，重写该类的方法表示接受到消息后如何消费
         * */
        channel.basicConsume("work_queue",true,new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                String message = new String(body,"UTF-8");
                System.out.println("消费者3，消息为："+message);
            }
        });

    }
}
